﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VTV.TaskManagementSystem.DataAccess
{
    public partial class Role : IConvertToLinqEntity<Role>, ISetting<Entities.Role>
    {
        public string ErrorMessage { get; set; }

        public Role Convert(object baseEntity)
        {
            Role result = null;
            try
            {
                if (baseEntity != null)
                {
                    var entity = (Entities.Role)baseEntity;

                    return result = new Role
                    {
                        Name = String.IsNullOrEmpty(entity.Name) ? null : entity.Name,
                        RoleID = entity.RoleID,
                        Descriptions = String.IsNullOrEmpty(entity.Descriptions) ? null : entity.Descriptions
                    };
                }
                else
                {
                    ErrorMessage = Common.Constant.ErrorMessage.SOURCE_IS_NULL;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }

            return result;
        }

        public bool AddNew(Entities.Role source)
        {
            try
            {
                var newSource = this.Convert(source);

                using (var data = new DataBaseDataContext())
                {
                    data.Roles.InsertOnSubmit(newSource);
                    data.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }

            return true;
        }

        public bool Delete(Entities.Role source)
        {
            try
            {
                var newSource = this.Convert(source);

                if (newSource != null)
                {
                    using (var data = new DataBaseDataContext())
                    {
                        data.Roles.Attach(newSource);
                        data.Roles.DeleteOnSubmit(newSource);
                        data.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.Role source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var Role = data.Roles.FirstOrDefault(p => p.RoleID == source.RoleID);
                    if (Role != null)
                    {
                        Role.Name = String.IsNullOrEmpty(source.Name) ? null : source.Name;
                        Role.Descriptions = String.IsNullOrEmpty(source.Descriptions) ? null : source.Descriptions;

                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        ErrorMessage = Common.Constant.SqlErrorMessage.NOT_EXIST_OBJECT_IN_DB;

                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public int? GetCount()
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    return data.Roles.Count();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Role> GetAll(int start, int limit)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var obj = data.Roles.OrderBy(p => p.Name).Skip(start).Take(limit).ToList();
                    var result = (from p in obj
                                  select new Entities.Role()
                                  {
                                      RoleID = p.RoleID,
                                      Name = String.IsNullOrEmpty(p.Name) ? string.Empty : p.Name,
                                      Descriptions = String.IsNullOrEmpty(p.Descriptions) ? string.Empty : p.Descriptions
                                  }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public Entities.Role GetRole(int RoleID)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var result = (from p in data.Roles
                                  where p.RoleID == RoleID
                                  select new Entities.Role
                                  {
                                      RoleID = p.RoleID,
                                      Name = p.Name,
                                      Descriptions = p.Descriptions
                                  }).FirstOrDefault();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }
    }
}
